﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.Domain;
using MShop.Core.DataAccess;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class CurrenyUnitController : SecureController
    {
        private readonly ICurrenyUnitDao _ICurrenyUnitDao;

        public CurrenyUnitController(ICurrenyUnitDao unitdao)
        {
            _ICurrenyUnitDao = unitdao;           
        }
        
        public ActionResult Index()
        {

            return View(_ICurrenyUnitDao.GetAll());
        }
        public ActionResult Create()
        {
           
            return View(new CurrenyUnit());
        }

        [HttpPost]
        public ActionResult Create(CurrenyUnit curreny)
        {
            
            if (ModelState.IsValid)
            {
                _ICurrenyUnitDao.Save(curreny);
                _ICurrenyUnitDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(curreny);
        }

        public ActionResult Edit(int id)
        {

            return View(_ICurrenyUnitDao.GetById(id));
        }


        public ActionResult Delete(int id)
        {
            var brand = _ICurrenyUnitDao.GetById(id);
            _ICurrenyUnitDao.Delete(brand);
            _ICurrenyUnitDao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(int id, CurrenyUnit curreny)
        { 
            if (ModelState.IsValid)
            {
                var dbcurreny = _ICurrenyUnitDao.GetById(id);
                UpdateModel<CurrenyUnit>(dbcurreny, new string[] { "Name", "Code", "Symbol","Rate" });
                _ICurrenyUnitDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(curreny);

            
        }
    }
}
